<?php
class Report extends MY_Controller
{
	public function gameLog() {
		try {
			ini_set('default_socket_timeout', -1);

			$key = 'hAnZawXZ75PHR4BkiG0vLAKHoBGpiFIP';
			$data = $this->param->getParam('data', array());
			$sign = $this->param->getParam('sign', array('method' => 'string', 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			$localSign = md5("{$data}{$key}");

			if($sign <> $localSign) {
				throw new Exception('校验签名错误');
			}
			//error_log("gameLog data = ".$data);
			$redis = new Redis();
			$redis->connect('127.0.0.1', 6379);
			$redis->auth("redispassword");
			$redis->rPush("ReportLog", $data);
			$redis->close();
			//error_log("redis finished ");
			$data = json_decode($data, true);
			if($data === false) {
				throw new Exception('数据格式错误');
			}
			switch ($data['module']) {
				case 'account':
					switch ($data['action']) {
						case 'register' :
							$sql = "
								insert into
									`t_data_game_account`
								(
									`gameAreaId`,
									`account`,
									`registerDateTime`,
                                    `roleName`,
									`registerIpAddress`
								)
								select
									a.`gameAreaId`,
									'{$data['username']}',
									'{$data['registerDateTime']}',
                                    '{$data['roleName']}',
									'{$data['registerIpAddress']}'
								from
									`t_data_game_area` a
								left join
									`t_data_operator` b
								on
									a.`operatorId` = b.`operatorId`
								where
									b.`operatorFlag` = '{$data['operatorFlag']}'
									and
									a.`areaNum` = '{$data['serverId']}'
									and
									a.`status` >= 4
							";
							//error_log("register sql = ".$sql);
							$this->gmDatabase->query($sql);
							
							if($this->gmDatabase->getAffectedRow() <= 0) {
								throw new Exception('游戏区不存在');
							}
							
							break;
							
						case 'login' :
						    //检查玩家是否已注册，未注册先补上注册
						    $sql = "select * from 
                                        t_data_game_account as  a,
                                        t_data_game_area as b,
                                        t_data_operator as c
                                    where 
                                        a.gameAreaId = b.gameAreaId
                                        and
    									a.`account` = '{$data['username']}'
    									and
    									b.`areaNum` = '{$data['serverId']}'
    									and
    									c.`operatorFlag` = '{$data['operatorFlag']}'
                            ";
						    //error_log("check sql = ".$sql);
						    $res = $this->gmDatabase->getRow($sql);
						    if(empty($res))
						    {
						        $sql = "
    								insert into
    									`t_data_game_account`
    								(
    									`gameAreaId`,
    									`account`,
    									`registerDateTime`,
                                        `roleName`,
    									`registerIpAddress`
    								)
    								select
    									a.`gameAreaId`,
    									'{$data['username']}',
    									'{$data['loginDateTime']}',
                                        '{$data['roleName']}',
    									'{$data['loginIpAddress']}'
    								from
    									`t_data_game_area` a
    								left join
    									`t_data_operator` b
    								on
    									a.`operatorId` = b.`operatorId`
    								where
    									b.`operatorFlag` = '{$data['operatorFlag']}'
    									and
    									a.`areaNum` = '{$data['serverId']}'
    									and
    									a.`status` >= 4
							     ";
						        $this->gmDatabase->query($sql);
						    }
						    
						    
							$sql = "
								insert into
									`t_data_game_account_login`
								(
									`userId`,
									`loginDateTime`,
									`loginIpAddress`
								)
								select
									a.`userId`,
									'{$data['loginDateTime']}',
									'{$data['loginIpAddress']}'
								from
									`t_data_game_account` a
								left join
									`t_data_game_area` b
								on
									a.`gameAreaId` = b.`gameAreaId`
								left join
									`t_data_operator` c
								on
									b.`operatorId` = c.`operatorId`
								where
									a.`account` = '{$data['username']}'
									and
									b.`areaNum` = '{$data['serverId']}'
									and
									c.`operatorFlag` = '{$data['operatorFlag']}'
							";
							
							$this->gmDatabase->query($sql);
							if($this->gmDatabase->getAffectedRow() <= 0) {
								throw new Exception('用户不存在');
							}
							
							break;
							
						default:
							throw new Exception('action不存在');
					}
					
					break;
					
				case 'role' :
					switch ($data['action']) {
						case 'create' :   //创角废弃，注册环节代替同时完成注册和创角
							$sql = "
								update
									`t_data_game_account` a
								left join
									`t_data_game_area` b
								on
									a.`gameAreaId` = b.`gameAreaId`
								left join
									`t_data_operator` c
								on
									b.`operatorId` = c.`operatorId`
								set
									roleName = '{$data['roleName']}'
								where
									c.`operatorFlag` = '{$data['operatorFlag']}'
									and
									b.`areaNum` = '{$data['serverId']}'
									and
									a.`account` = '{$data['username']}'
							";
							//error_log("create sql = ".$sql);
							$this->gmDatabase->query($sql);
							
							if($this->gmDatabase->getAffectedRow() <= 0) {
								throw new Exception('用户不存在');
							}
							
							break;
						default:
							throw new Exception('action不存在');
					}
					
					break;
					
				case 'online' :
					switch ($data['action']) {
						case 'detail' :
							$sql = "
								insert into
									`t_data_game_online`
								(
									`gameAreaId`,
									`statDateTime`,
									`onlineRoleNum`
								)
								select
									a.`gameAreaId`,
									'{$data['statDateTime']}',
									'{$data['onlineRoleNum']}'
								from
									`t_data_game_area` a
								left join
									`t_data_operator` b
								on
									a.`operatorId` = b.`operatorId`
								where
									b.`operatorFlag` = '{$data['operatorFlag']}'
									and
									a.`areaNum` = '{$data['serverId']}'
									and
									a.`status` >= 4
							";
							
							$this->gmDatabase->query($sql);
							
							//记录心跳数据
                            $sql = "select 
                                        a.`status` 
                                    from 
                                        t_data_game_area as a, 
                                        t_data_operator as b 
                                    where 
                                        a.areaNum = '{$data['serverId']}' 
                                        and 
                                        a.operatorId = b.operatorId 
                                        and 
                                        b.operatorFlag = '{$data['operatorFlag']}'";
                            $status = $this->gmDatabase->getColumn($sql);
                            //error_log(json_encode($status));
                            if($status >= 4)
                            {                                
    							$sql = "insert into 
                                            `t_data_server_heart` 
                                        (   
                                            `operatorFlag`, 
                                            `sid`, 
                                            `time`
                                        ) 
                                        values 
                                        (   
                                            '{$data['operatorFlag']}', 
                                            '{$data['serverId']}', 
                                            now()
                                        ) 
                                        ON DUPLICATE KEY UPDATE 
                                            `time`=now()";
    							$this->gmDatabase->query($sql);
                            }
							
							if($this->gmDatabase->getAffectedRow() <= 0) {
								throw new Exception('游戏区不存在');
							}
							
							break;
							
						default:
							throw new Exception('action不存在');
					}
					
					break;
				
				case 'pay' :
					switch ($data['action']) {
						case 'detail' :
							$sql = "
								insert into
									`t_data_game_pay`
								(
									`userId`,
									`orderId`,
									`money`,
									`gold`,
									`payDateTime`
								)
								select
									a.`userId`,
									'{$data['orderId']}',
									'{$data['money']}',
									'{$data['gold']}',
									'{$data['payDateTime']}'
								from
									`t_data_game_account` a
								left join
									`t_data_game_area` b
								on
									a.`gameAreaId` = b.`gameAreaId`
								left join
									`t_data_operator` c
								on
									b.`operatorId` = c.`operatorId`
								where
									a.`account` = '{$data['username']}'
									and
									b.`areaNum` = '{$data['serverId']}'
									and
									c.`operatorFlag` = '{$data['operatorFlag']}'
							";
							
							$this->gmDatabase->query($sql);
							
							if($this->gmDatabase->getAffectedRow() <= 0) {
								throw new Exception('角色不存在');
							}
							
							$sql = "
								update
									`t_data_game_account` a
								left join
									`t_data_game_area` b
								on
									a.`gameAreaId` = b.`gameAreaId`
								left join
									`t_data_operator` c
								on
									b.`operatorId` = c.`operatorId`
								set
									a.`totalPayMoney` = a.`totalPayMoney` + {$data['money']}
								where
									a.`account` = '{$data['username']}'
									and
									b.`areaNum` = '{$data['serverId']}'
									and
									c.`operatorFlag` = '{$data['operatorFlag']}'
							";
							$this->gmDatabase->query($sql);
							
							break;
						case "monthCard":
						    $sql = "
								insert into
									`t_data_month_card`
								(
									`userId`,
									`monthCardType`,
									`operatorDateTime`
								)
								select
									a.`userId`,
									'{$data['monthCardType']}',
									'{$data['operatorDateTime']}'
								from
									`t_data_game_account` a
								left join
									`t_data_game_area` b
								on
									a.`gameAreaId` = b.`gameAreaId`
								left join
									`t_data_operator` c
								on
									b.`operatorId` = c.`operatorId`
								where
									a.`account` = '{$data['username']}'
									and
									b.`areaNum` = '{$data['serverId']}'
									and
									c.`operatorFlag` = '{$data['operatorFlag']}'
							";
						    //error_log("card sql = ".$sql);
						    $this->gmDatabase->query($sql);
						    
						    if($this->gmDatabase->getAffectedRow() <= 0) {
						        throw new Exception('角色不存在');
						    }
						    break;
						default:
							throw new Exception('action不存在');
					}
														
					break;
					
				case 'currency':
					break;
				case 'item':
					break;
				default:
					throw new Exception('module不存在');
			}
			echo 'Success';
		}
		
		catch(Exception $e) {
			log_message('debug', $e->getMessage());
			echo 'Fail';
		}
	}
}